/*
 * Copyright (C) 2012 BARBOTIN Nicolas
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#ifndef GCPROJECT_H
#define GCPROJECT_H

#include <QtGui>
#include <Phonon/MediaSource>
#include <Phonon/MediaObject>
#include <Phonon/AudioOutput>
#include "GCTreeWidget.h"
#include "GCDataReader.h"
#include "GCStructs.h"
#include "GCCodeBuilder.h"

class GCDataReader;

/* GCProject class */

namespace Ui {
    class GCProject;
}

class GCProject : public QWidget
{
    Q_OBJECT

public:
    GCProject(GCTreeWidget *tree, GCDataReader *d, QString name, QWidget *parent = 0);
    ~GCProject();
    QString getProjectName() { return prjName; }
    QString getIGWindowTitle() { return gameWinTitle; }
    QPoint getRoomSize() { return QPoint(gameWinWidth, gameWinHeight); }
    int getStartRoom() { return startRoom; }
    bool isCursorVisible() { return cursorVisible; }

    int countSprites() { return sprites.count(); }
    int countSounds() { return sounds.count(); }
    int countScripts() { return scripts.count(); }
    int countBackgrounds() { return backgrounds.count(); }
    int countObjects() { return objects.count(); }
    int countRooms() { return rooms.count(); }
    int countFonts() { return fonts.count(); }

    GCSprite getSprites(int i) { return sprites[i]; }
    GCSound getSounds(int i) { return sounds[i]; }
    GCScript getScripts(int i) { return scripts[i]; }
    GCBackground getBackgrounds(int i) { return backgrounds[i]; }
    GCObject getObjects(int i) { return objects[i]; }
    GCRoom getRooms(int i) { return rooms[i]; }
    QString getFonts(int i) { return fonts[i]; }
    GCActionParam *getAParam(int i, int j, int k, int l) { return &objects[i].events[j].actions[k].params[l]; }
    GCActionParam *getEParam(int i, int j, int k) { return &objects[i].events[j].params[k]; }

    void addSprite(GCSprite d) { sprites.append(d); }
    void addSound(GCSound d) { sounds.append(d); }
    void addScript(GCScript d) { scripts.append(d); }
    void addBackground(GCBackground d) { backgrounds.append(d); }
    void addObject(GCObject d) { objects.append(d); }
    void addRoom(GCRoom d) { rooms.append(d); }
    void addNewFont(QString uri) { fonts.append(uri); }
    void setStartRoom(int i) { startRoom = i; }
    void setCursorVisible(bool v) { cursorVisible = v; }

    void updateNodes();
    void updateProps(QString pn, QString pt, int pw, int ph);
    void setProjectLocation(QString l) { prjLoc = l; }
    QString getProjectLocation() { return prjLoc; }
    bool getSaved() { return saved; }
    void setSaved(bool s) { saved = s; }
    void cancelTree();

    void compile(bool r = false);

protected:
    void closeEvent(QCloseEvent *e);

signals:
    void projectRemoved();

private:
    Ui::GCProject *ui;
    QTreeWidget *tree;
    QString prjName;
    QString gameWinTitle;
    QString prjLoc;
    QString compLoc;
    int gameWinWidth;
    int gameWinHeight;
    int startRoom;
    bool cursorVisible;
    bool saved;

    QMenu *treeMenu;
    QAction *tma_del;
    QAction *tma_dup;
    QTreeWidgetItem *menuItem;

    QTreeWidgetItem *projectNode;
    QTreeWidgetItem *spritesNode;
    QTreeWidgetItem *objectsNode;
    QTreeWidgetItem *soundsNode;
    QTreeWidgetItem *scriptsNode;
    QTreeWidgetItem *backgroundsNode;
    QTreeWidgetItem *levelsNode;
    QTreeWidgetItem *projectSettings;
    QList<GCSprite> sprites;
    QList<GCSound> sounds;
    QList<GCScript> scripts;
    QList<GCBackground> backgrounds;
    QList<GCRoom> rooms;
    QStringList fonts;

    QList<GCObject> objects;
    GCDataReader *dr;

    QList<QWidget*> curWidgets;

    Phonon::MediaObject *mediaObj;
    Phonon::AudioOutput *audioOut;

    int editingSpriteIndex;
    int editingSoundIndex;
    int editingObjectIndex;
    int editingScriptIndex;
    int editingBgIndex;
    int editingRoomIndex;
    bool editingParams;
    QTimer *spriteTimer;

    void setupTree(QTreeWidget *t);
    void updateSpritesCombo();
    void updateBgCombo();
    void updateObjectsCombo();
    void objectUpdateActionParams(EnumParamType cPT, int nodeId);
    void scanItem(int *it, int *ii);
    void updateFontCombo();
    void updateParentCombo();

    QString compileEvents(int i, QString cat);
    QString compileEvent(int i, int j, QString cat);
    QString compileRoomObj(int i, int j);
    void parseParams(bool isAction, QString *cActionCode, int i, int j, int k = -1);

private slots:
    void newSprite();
    void newSpriteImage();
    void spriteImageChange(int frame);
    void spriteNextImage();
    void spriteFPSChange(int value);
    void spritePlayStop(bool playOrStop);
    void spriteChangeName(QString newName);
    void spriteRemoveImage();

    void newSound();
    void soundLoad();
    void soundPlayStop(bool playOrStop);
    void soundChangeVolume(int volume);
    void soundStop();
    void soundChangeName(QString newName);
    void soundChangePlayMode(int playMode);
    void soundCheckRepeat();

    void newScript();
    void scriptChangeName(QString newName);

    void newBackground();
    void bgChangeName(QString newName);
    void bgSetType(bool type);
    void bgSetColor(QColor col);
    void bgLoadImage();
    void bgSetRepeated(bool rep);

    void newRoom();
    void roomChangeName(QString newName);
    void roomChangeBg(int i);
    void roomSetObject(int i);
    void roomObjAdded(int x, int y, int z);
    void roomObjRemoved(int id);

    void newObject();
    void objectChangeName(QString newName);
    void objectChangeSprite(int newId);
    void objectAddEvent();
    void objectSelEventChange(int index);
    void objectEventSelParams(QListWidgetItem *);
    void objectAddAction();
    void objectSelActionChange(int index);
    void objectActionSelParams(QListWidgetItem *);
    void objectRemoveEvent();
    void objectRemoveAction();
    void objectSelectParent(int index);

    void currentFontChanged(int id);
    void addFont();
    void removeFont();
    void changeStartRoom(int id);
    void changeCursorVisible(bool v);

    void loadItem(QTreeWidgetItem *item, int);
    void treeContextMenu(QTreeWidgetItem *item, QPoint pos);
    void requestItemRemove();
    void requestItemDuplication();

public slots:
    void mainPage();
    void changeProjectName(QString n);
    void changeWindowTitle(QString t);
    void changeWindowWidth(int x);
    void changeWindowHeight(int y);
    void run();
};

#endif // GCPROJECT_H
